package com.yinchuang.aiuidemo;

import android.os.Bundle;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;

import com.iflytek.aiui.AIUIConstant;
import com.iflytek.aiui.AIUIMessage;
import com.ynh.mediaclient.jni.YNHAudioRecordJni;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 */
public class PcmRecordActivity extends AppCompatActivity implements AIUImanager.AIUIResultEvent {
    public static String TAG = "PcmRecordActivity";
    @BindView(R.id.tv)
    AppCompatTextView mTv;
    @BindView(R.id.btn_start)
    AppCompatButton mBtnStart;
    @BindView(R.id.btn_stop)
    AppCompatButton mBtnStop;
    // 语音识别对象
    private SpeechRecognizer mAsr;

    private YNHAudioRecordJni mYNHAudioRecordJni;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pcm);
        ButterKnife.bind(this);
        AIUImanager.setAIUIResultEvent(this);
        aiuImanager = AIUImanager.getInstance(this);
        aiuImanager.setText(mTv);
        mYNHAudioRecordJni = new YNHAudioRecordJni();
    }

    private AIUImanager aiuImanager;

    @OnClick({R.id.btn_start, R.id.btn_stop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                mTv.setText("");
                startRecord();
                break;
            case R.id.btn_stop:
                stopRecord();
                break;
        }
    }

    @Override
    public void onSuccess(final String result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTv.setText(result);
            }
        });
    }

    private Thread mThread;

    private void startRecord() {
        Log.d(TAG, "startRecord");
        isStop = false;
        mThread = new Thread(new RecordThread());
        mThread.start();
    }

    private void stopRecord() {
        Log.d(TAG, "stopRecord");
        mYNHAudioRecordJni.stop();
        isStop = true;
        AIUIMessage msg = new AIUIMessage(AIUIConstant.CMD_STOP_WRITE, 0, 0, params, null);
        aiuImanager.sendMessage(msg);
    }

    private volatile boolean isStop = false;
    private String params = "data_type=audio,sample_rate=16000";
    private byte[] buffer;

    class RecordThread implements Runnable {

        @Override
        public void run() {
            Log.d("zcl", " start record!");
            mYNHAudioRecordJni.start();
            int size = mYNHAudioRecordJni.getBufferSize();
            Log.d(TAG, "size :" + size);
            buffer = new byte[size];
            while (!isStop) {
                int leng = mYNHAudioRecordJni.read(buffer, size);
                Log.d(TAG, "leng is " + leng);
                if (leng != 0) {
                    aiuImanager.sendMessage(new AIUIMessage(AIUIConstant.CMD_WRITE, 0, 0, params, buffer));
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mYNHAudioRecordJni.destory();
    }
}
